package controllers;

import java.util.Date;
import java.util.List;
import models.Account;
import models.BankStatement;
import models.parsers.HsbcCreditParser;
import play.Logger;
import play.data.Upload;
import play.mvc.Controller;
import utils.DateUtil;

public class Application extends Controller {

    public static void index(String transactionsCsv) {
        List<Account> activeAccounts = Account.findAll();
        String statementLabel = createDefaultStatementLabel();
        render(transactionsCsv, activeAccounts, statementLabel);
    }

    private static String createDefaultStatementLabel() {
        return DateUtil.formatDate(new Date(), "yyyyMMdd_HHMMSS");
    }

    public static void uploadFile(Upload data, String statementLabel, String accountClabe) {

        Logger.info("uploading: " + statementLabel);
        Logger.info("accountClabe: " + accountClabe);
        Logger.info(data.getContentType());
        Logger.info(data.getFieldName());
        Logger.info(data.getFileName());
        HsbcCreditParser parser = new HsbcCreditParser(data.asStream());
        BankStatement statement = parser.getStatement();
        statement.account = Account.find("byClabe", accountClabe).first();
        statement.fixBasicDataInAllTransactions();
        statement.label = statementLabel;
        statement.save();

        Account accountCheck = Account.find("byClabe", accountClabe).first();
        Logger.info("transactions:" + accountCheck.transactions.size());
        index(statement.toString());
    }
}